package com.gkym.es.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.UnknownHostException;

/**
 * [ Elasticsearch控制层 ]
 *
 * @author Love丶TG
 * @version 1.0.0
 * @create 2020 年 01 月 14 日 16:13
 */
@Slf4j
@RestController
public class EsController {

//    @Autowired
//    private RestHighLevelService restHighLevelService;

    @GetMapping("tty")
    public void das() throws UnknownHostException {


        /// TransportClient测试使用

//        System.err.println("==========");
////        Settings settings = Settings.builder().put("cluster.name", "GKYM_ES")    //指定集群名称
////                .put("client.transport.sniff", true)                //启动嗅探功能
////                .build();
////        TransportClient client = new PreBuiltTransportClient(settings)
////                .addTransportAddresses(new TransportAddress(InetAddress.getByName("127.0.0.1"),
////                        9300));
//        ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
//        System.out.println(response.getState());
//        boolean exists = client.admin().indices().prepareExists().execute().actionGet().isExists();
//        System.err.println(exists);
//        System.err.println(client.admin().indices().prepareDelete("ty").execute().actionGet().isAcknowledged());
//        client.close();


        /// 测试工具类可用

//        /// 删除索引
//        boolean tt = restHighLevelService.deleteIndex("twitter");
//        System.err.println(tt);

//        Map<String, String> message = new HashMap<>();
//        message.put("id", "2");
//        BulkResponse insert = restHighLevelService.insert("ty", "text", message);
//        System.err.println(insert);
        //这边涉及到业务代码，就不放全了了，简单放几个解析的方法
//        SearchResponse response = getClient().page(...);
//        if (null != response) {
//            //取到hits
//            SearchHit[] hits = response.getHits().getHits();
//            if (null != hits) {
//                for (SearchHit hit : hits) {
//                    //这就是取到的数据的Json格式数据
//                    String source = hit.getSourceAsString();
//                    //使用Gson转换成Bean放进结果list中
//                    T obj = GsonUtils.read(source, clazz);
//                    resultList.add(obj);
//                }
//            }
//        }
    }

/// 以下为RestHighLevelClient的简单测试创建索引
/// 参考博文：https://www.cnblogs.com/betterwgo/p/11268869.html

//    @Resource
//    private RestHighLevelClient esClient;
//
//    @GetMapping("tty")
//    public void creatA() {
//        CreateIndexRequest request = new CreateIndexRequest("tty");
////        Map<String, Object> message = new HashMap<>();
////        message.put("type", "text");
////        Map<String, Object> properties = new HashMap<>();
////        properties.put("message", message);
////        Map<String, Object> mapping = new HashMap<>();
////        mapping.put("properties", properties);
////        request.mapping(String.valueOf(mapping));
//        try {
//            XContentBuilder builder = XContentFactory.jsonBuilder();
//            builder.startObject();
//            {
//                builder.startObject("properties");
//                {
//                    builder.startObject("message");
//                    {
//                        builder.field("type", "text");
//                    }
//                    builder.endObject();
//                }
//                builder.endObject();
//            }
//            builder.endObject();
//            request.mapping(String.valueOf(builder));
//            esClient.indices().createAsync(request, RequestOptions.DEFAULT, listener);
////            CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);
////            System.err.println(createIndexResponse);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    ActionListener<CreateIndexResponse> listener =
//            new ActionListener<CreateIndexResponse>() {
//
//                @Override
//                public void onResponse(CreateIndexResponse createIndexResponse) {
//                    System.err.println(createIndexResponse);
//                }
//
//                @Override
//                public void onFailure(Exception e) {
//
//                }
//            };
}
